package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController("userAddressBookController")
@RequestMapping("/user/addressBook")
@Api(tags = "C端-地址簿接口")
@Slf4j
public class AddressBookController {
	@Autowired
	private AddressBookService addressBookService;

	@PostMapping
	@ApiOperation("添加地址")
	public Result<Object> add(@RequestBody AddressBook addressBook) {
		addressBook.setUserId(BaseContext.getCurrentId());
		addressBookService.save(addressBook);
		return Result.success();
	}

	@GetMapping("/list")
	@ApiOperation("查看所有地址")
	public Result<Object> list() {
		AddressBook addressBook = new AddressBook();
		addressBook.setUserId(BaseContext.getCurrentId());
		List<AddressBook> list = addressBookService.list(addressBook);
		return Result.success(list);
	}

	@GetMapping("/{id}")
	@ApiOperation("查看指定地址")
	public Result<Object> get(@PathVariable Long id) {
		AddressBook addressBook = addressBookService.getById(id);
		return Result.success(addressBook);
	}

	@PutMapping
	@ApiOperation("修改地址")
	public Result<Object> update(@RequestBody AddressBook addressBook) {
		addressBookService.update(addressBook);
		return Result.success();
	}

	@PutMapping("/default")
	@ApiOperation("设置默认地址")
	public Result<Object> updateDefault(@RequestBody AddressBook addressBook) {
		addressBookService.updateDefault(addressBook);
		return Result.success();
	}

	@GetMapping("/default")
	@ApiOperation("h获取默认地址")
	public Result<AddressBook> queryDefault() {
		AddressBook addressBook = addressBookService.queryDefault();
		return Result.success(addressBook);
	}
}
